﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace Database
{
    /// <summary>
    /// 地图地址数据库
    /// </summary>
    public class MapDatabase
    {
        private static Address Address风木园 = new Address("风木园",new byte[]{0xA4,0x43});
        private static Address Address码头废墟 = new Address("码头废墟", new byte[] { 0x02, 0x33 });

        private static Dictionary<string, Address> addressDict = new Dictionary<string, Address>(256);
        private static List<Address> addressList = null;

        public static void Init()
        {
            Console.WriteLine("开始加载地图数据库...");
            Load();

            if (addressList == null)
            {
                #region 程序内置任务数据
                addressList = new List<Address>(256);
				
				Address风木园.FatherAddress = new Address("雪边峰",new byte[]{0xA4,0x12,0x01,0x00});
                addressList.Add(Address风木园);
                addressList.Add(Address码头废墟);
                #endregion

                Save();
            }

            foreach (Address m in addressList)
            {
                addressDict.Add(m.Name, m);
                Console.WriteLine("地图："+m.Name+"\tCode:"+BitConverter.ToString(m.Code));
            }
            Console.WriteLine("加载地图数据库完成！");
        }

        /// <summary>
        /// 根据名称获取地图
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static Address GetAddress(string name)
        {
            return addressDict[name];
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//data//address.xml";
            try
            {
                using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Address>));
                    serializer.Serialize(stream, addressList);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("保存地图数据库出错，错误信息：");
                Tools.PrintException(ex);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public static void Load()
        {
            try
            {
                using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\data\\address.xml", FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Address>));
                    addressList = (List<Address>)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("加载地图数据库出错，已采用程序内置数据，错误信息：");
                Tools.PrintException(ex);
            }
        }
    }
}
